import { IS_SUBAPP, useDrsState, useLocation } from 'umi';

/**
 * 主应用 appCode 取自路由，子应用取自 useDrsState
 * @returns
 */
export default function useAppCode() {
  const { appCode: drsAppCode, userInfo } = useDrsState();
  const location = useLocation();
  const searchParams = new URLSearchParams(location?.search);
  const query = (location?.state ?? {
      appCode: searchParams.get('appCode'),
      appName: searchParams.get('appName'),
    } ??
    {}) as { appCode: string; appName: string };

  const appCode = IS_SUBAPP ? drsAppCode : query?.appCode;
  const appName = IS_SUBAPP ? userInfo?.appName : query.appName;

  return {
    /** 所配置的应用标识 */
    appCode,
    appName,
    /** drs 当前访问系统标识 */
    userAppCode: drsAppCode,
  };
}
